package com.artron.mediaartron.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.artron.mediaartron.R;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-23.
 */

public class AddAddressItem extends LinearLayout implements View.OnClickListener {

    private TextView mTvName;
    private EditText mEtText;

    public AddAddressItem(Context context) {
        this(context, null);
    }

    public AddAddressItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddAddressItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AddAddressItem);
        String name = ta.getString(R.styleable.AddAddressItem_name);
        boolean isNanClick = ta.getBoolean(R.styleable.AddAddressItem_nanClick, false);

        init(name, isNanClick);
    }

    protected void init(String name, boolean isNanClick) {
        View inflate = LayoutInflater.from(getContext()).inflate(
                R.layout.layout_add_address_item, this, true);

        mTvName = ((TextView) inflate.findViewById(R.id.LayoutAddAddress_tv_name));
        mEtText = ((EditText) inflate.findViewById(R.id.LayoutAddAddress_et_text));

        inflate.setOnClickListener(this);
        setName(name);
        if (isNanClick) {
            setNaNClick();
            inflate.findViewById(R.id.LayoutAddAddress_iv_go).setVisibility(View.VISIBLE);
        }
    }

    public void setName(String name) {
        mTvName.setText(name);
    }

    public void setText(String text) {
        mEtText.setText(text);
    }

    public void setNaNClick() {
        mEtText.setClickable(false);
        mEtText.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ((ViewGroup) mEtText.getParent()).onTouchEvent(event);
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (mEtText != null) {
            mEtText.requestFocus();
        }
    }

    public String getText() {
        if (mTvName != null) {
            return mEtText.getText().toString();
        }
        return "";
    }

    public void setInputType(int type) {
        mEtText.setInputType(type);
    }

}
